package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.PermissionMapper;
import com.vehicle.pojo.Permission;
import com.vehicle.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImp implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Permission findPermissionById(Integer pid) {
        return permissionMapper.selcetPermissionsById(pid);
    }

    //分页查询permission
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public DataGridView findPermissionByPage(Permission permission) {
        Page<Object> page = PageHelper.startPage(permission.getPage(), permission.getLimit());
        List<Permission> permissions=permissionMapper.selectPermissionByPage(permission);
        DataGridView dataGridView=new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(permissions);
        return dataGridView;
    }

    //查询所有权限
    @Override
    public List<Permission> findPermissions() {
        return permissionMapper.selectPs();
    }
    //根据rid查询权限pid集合
    @Override
    public List<Integer> findPids(Integer rid) {
        List<Permission> permissions = permissionMapper.selectPermissionsWithStep(rid);
        List<Integer> pids=new ArrayList<>();
        for (Permission p:permissions
             ) {
            pids.add(p.getPid());
        }
        return pids;
    }
}
